import nme.geom.Rectangle;
import nme.geom.Point;
import nme.geom.Matrix;

import nme.display.BitmapData;
import nme.display.Graphics;
import nme.display.Sprite;

import nme.text.TextFieldAutoSize;
import nme.text.TextField;
import nme.text.TextFormat;
import nme.Vector;
import nme.Assets;
import nme.events.Event;
import nme.events.MouseEvent;

class Label extends Sprite// implements IGameObject
{
	private static var _textfield:TextField = new TextField ();
	private static var _listLabel:Array<Label> = new Array<Label> ();
	
	private static var MAX_AVIABLE_LABEL:Int = 200;	
	
	private var _text:String;
	private var _format:TextFormat;
	private var _texture:BitmapData;
	private var _maxWidth:Float;
	private var _matrix:Matrix;
	private var _anchor:Int;
	
	private var _is_not_use:Bool = true;	
	private var _has_changed:Bool = true;
	
	private function new ()
	{
		super ();
		addEventListener (Event.ENTER_FRAME, onEnterFrame);
	}
	
	public static function create (text:String, format:TextFormat = null, anchor:Int = 0, width:Float = -1):Label
	{
		var label:Label = null;
		for (index in 0 ... _listLabel.length)
		{
			if (_listLabel [index]._is_not_use)
			{
				label = _listLabel [index];
				break;
			}
		}
		
		if (label == null)
		{
			label = new Label ();
			if (_listLabel.length < MAX_AVIABLE_LABEL)
			{
				_listLabel.push (label);
			}
		}
		
		label._is_not_use = false;
		label._matrix = new Matrix (1, 0, 0, 1, 0, 0);
		
		label.setText (text);
		label.setFormat (format);
		label.setWidth (width);
		label.setAnchor (anchor);
		label.makeTexture ();
		
		return label;
	}
	
	private function onEnterFrame(event:Event):Void
	{
		if (_has_changed) makeTexture ();
	}
	
	public function setWidth (width:Float):Void
	{
		_maxWidth = width;
		_has_changed = true;
	}
	
	public function setText (text:String):Void
	{
		_text = text;
		_has_changed = true;
	}	
	
	public function setOpacity(value:Float):Void
	{
		_has_changed = true;
	}
	
	public function setFormat (format:TextFormat):Void
	{
		_format = format;
		_has_changed = true;
	}
	
	public function setAnchor (anchor:Int):Void
	{
		_anchor = anchor;
		_has_changed = true;
	}
	
	public function release ():Void
	{
		_is_not_use = true;
		_has_changed = true;
	}
	
	public function makeTexture ():Void
	{
		if (_format == null) _format = Fonts.TITLE_BLACK_20;
		_textfield.defaultTextFormat = _format;
		_textfield.x = 0;
		_textfield.y = 0;
		_textfield.htmlText = _text;
		_textfield.embedFonts = false;
		_textfield.selectable = false;
		
		if (_maxWidth <= 0)
			_textfield.autoSize = TextFieldAutoSize.LEFT;
		else
		{
			_textfield.autoSize = TextFieldAutoSize.NONE;
			_textfield.width = _maxWidth;
		}
		_texture = new BitmapData (Std.int(_textfield.width), Std.int(_textfield.height), true, 0x000000);
		_texture.draw (_textfield);
		
		if ((_anchor & Anchor.HCENTER) != 0) _matrix.tx = -_textfield.width * 0.5;
		else if ((_anchor & Anchor.RIGHT) != 0) _matrix.tx = -_textfield.width;
		
		if ((_anchor & Anchor.VCENTER) != 0) _matrix.ty = -_textfield.height * 0.5;
		else if ((_anchor & Anchor.BOTTOM) != 0) _matrix.ty = -_textfield.height;
		
		graphics.clear ();
		graphics.beginBitmapFill (_texture, _matrix, false, true);
		// graphics.lineStyle (1.0, 0x00FF00, 1);
		graphics.drawRect (_matrix.tx, _matrix.ty, _texture.width, _texture.height);
		graphics.endFill();
		_has_changed = false;
	}
}